<template>
  <el-dialog :append-to-body="true" :close-on-click-modal="false" :visible.sync="dialog" :title="form.userName + '(用户信息)'" width="500px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="160px">
      <el-form-item label="上级" prop="mgrAccount">
        <el-input :readonly="true" v-model="form.mgrAccount" :clearable="true" placeholder="请选择上级">
          <el-button slot="append" icon="el-icon-search" @click="selectUser" />
        </el-input>
      </el-form-item>
      <el-form-item label="职务" prop="jobId">
        <el-input :readonly="true" v-model="form.jobName" :clearable="true" placeholder="请选择职务">
          <el-button slot="append" icon="el-icon-search" @click="selectUserJob" />
        </el-input>
      </el-form-item>
      <el-form-item label="用户状态" prop="userStatus">
        <el-select v-model="form.userStatus" placeholder="请选状态">
          <el-option
            v-for="(item) in dicts.user_status"
            :key="item.id"
            :label="item.dictName"
            :value="item.dictCode"/>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
    <userSelectList ref="userSelectList" :mult_select="false" @userSelectCallback="userSelectCallback" />
    <jobSelectList ref="jobSelectList" :mult_select="false" @jobSelectCallback="jobSelectCallback" />
  </el-dialog>
</template>

<script>
import { doSaveUserInfo } from '@/api/user'
import userSelectList from '@/views/system/user/module/userSelectList'
import jobSelectList from '@/views/system/sysJob/module/jobSelectList'

export default {
  components: { userSelectList, jobSelectList },
  dictTypes: ['user_status'],
  props: {
    sup_this: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      loading: false,
      dialog: false,
      form: {
        userName: null,
        userAccount: '',
        userId: '',
        mgrAccount: '',
        jobId: '',
        jobName: '',
        enableFlag: 'Y',
        userStatus: ''
      },
      rules: {
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    init(userRow) {
      this.form = userRow
    },
    selectUser() {
      const _this = this.$refs.userSelectList
      _this.init()
      _this.dialog = true
    },
    selectUserJob() {
      const _this = this.$refs.jobSelectList
      _this.init()
      _this.dialog = true
    },
    jobSelectCallback(selectedRow, jobSelectObj) {
      this.form.jobId = ''
      this.form.jobName = ''
      if (selectedRow) {
        this.form.jobId = selectedRow.id
        this.form.jobName = selectedRow.jobName
      }
      jobSelectObj.cancel()
      jobSelectObj.loading = false
    },
    userSelectCallback(selectedRow, userSelectObj) {
      this.form.mgrAccount = ''
      if (selectedRow) {
        this.form.mgrAccount = selectedRow.userAccount
      }
      userSelectObj.cancel()
      userSelectObj.loading = false
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          doSaveUserInfo(this.form).then(res => {
            this.resetForm()
            this.loading = false
            this.sup_this.init()
          }).catch(err => {
            this.loading = false
            console.log(err.response)
          })
        }
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
    }
  }
}
</script>

<style scoped>

</style>
